package chapter4.RPC;

import com.rabbitmq.client.*;
import util.Conn;

import java.io.IOException;

/**
 * Created by uu on 2017/6/21.
 */
public class rpc_server {
    public static void main(String[] args) throws Exception{
        Connection conn = new Conn().getConnection();
        final Channel channel = conn.createChannel();
        channel.exchangeDeclare("rpc","direct",true,false,null);
        channel.queueDeclare("ping",true,false,false,null);
        channel.queueBind("ping","rpc","ping");
        channel.basicConsume("ping",false,"ping",new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String routingKey = envelope.getRoutingKey();
                String contentType = properties.getContentType();
                long deliveryTag = envelope.getDeliveryTag();
                // (process the message components here ...)
                System.out.println("Received API call...replying..."+new String(body,"UTF-8"));
                channel.basicPublish("",properties.getReplyTo(),properties,body);
                channel.basicAck(deliveryTag, false);

            }
        });
    }
}
